$(document).ready(function(){
	getActivity();
});

function getActivity(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/marking-img/get-marking-img',
            type:'post',
            data:function(d){
            },
            error:getError
        },
        aoColumns: [
            { "data": "id", "bSortable": true },
            { "data": "img_name", "bSortable": false },
            { "data": "sort", "bSortable": true },
            { "data": "img_url", "bSortable":false },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [3],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<img src="'+imageBathUrl+full.img_url +'"/>';
                }
            },
            {
                "targets": [4],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return '<a href="javascript:" onclick="markingImgDetail('+ full.id +')">修改</a> | <a href="javascript:" onclick="markingImgDelete('+ full.id +','+(full.use_flg != 1 ? 1 : 0)+')">删除</a>';
                }
            }
        ],
        order: [[ 0, "desc"]]
    });
}

function table_list_reload(){
	var table = $('#table_list').DataTable();
    table.ajax.reload();
}

function markingImgDetail(id){
	$.post('/marking-img/marking-img-detail',{id:id},success,'json');
	return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
        	$('#marking-img-modal').modal('show');
        	var result = data.val;
        	$('#img_name').val(result.img_name);
        	$('#sort').val(result.sort);
        	if(result.img_url){
        		$('#img_url').val(result.img_url);
        		$('#img_url').prev().removeClass('hide');
        		$('#img_url').prev().attr('src',imageBathUrl+result.img_url);
        	}
        	$('#product_url').val(result.img_url);
        	$('#marking_img_id').val(result.id);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
}

function markingImgDelete(id,use_flg){
	title = '确定删除此打标么？';
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/marking-img/change-marking-img-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200){
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }
    });
}

$('#marking_img_form').delegate('.upload-img-btn','click',function(){
    $('.upload_file_ot').trigger('click');
});

$('.upload_file_ot').change(function(){
    if(this.value != ''){
    	$('#upload_form_update').submit();
    }
});

$('#upload_form_update').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
            	$('#img_url').val(result.val);
            	$('#img_url').prev().removeClass('hide');
            	$('#img_url').prev().attr('src',imageBathUrl+result.val);
            	$('input[type=file]').closest('form').get(0).reset();
            }
        }
    });
});

$('.ajax_post_tag').on('click',function(){
	var form = $('#marking_img_form');
    var img_name = $('#img_name').val();
    if(img_name == ''){
        $.jBox.error('请填写图片名称', '错误');
        return false;
    }
	var img_url = $('#img_url').val();
	if(img_url == ''){
        $.jBox.error('请上传一张商品打标图片', '错误');
        return false;
    }
    $.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200){
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#marking-img-modal').modal('hide');
                table_list_reload();
            }, 1000);
        }else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('.add-marking-img').on('click',function(){
	$('#marking-img-modal').modal('show');
	$('#img_name').val('');
	$('#sort').val('');
	$('#img_url').val('');
	$('#img_url').prev().addClass('hide');
	$('#marking_img_id').val('');
});